(function() {
    'use strict';
    angular.module('services.userBehaviorAnalysis.srPerceptionUwrAnalysis')
        .factory('srPerceptionRuqAnalysisService', srPerceptionRuqAnalysisService);

    function srPerceptionRuqAnalysisService(Restangular, CoreService) {
        return {
            srPerceptionRuqAnalysisQuery: srPerceptionRuqAnalysisQuery,
            getDimensions: getDimensions,
            getName: getName,
            fmtSendData: fmtSendData,
            fmtDateTitle: fmtDateTitle,
            getEchartsData: getEchartsData,
            getEchartsPieData: getEchartsPieData,
            exportView: exportView,
            getFormatterFn: getFormatterFn,
            formatterYAxis: formatterYAxis
        };
        //请求方法
        function srPerceptionRuqAnalysisQuery(params, callback) {
            Restangular.all('sas/analysisUserBehavior/apperceiveChurnRate/queryChurnNum').post(params).then(callback);
        }

        //统计维度
        function getDimensions() {
            return [
                { dimension: 't', dimensionName: '按日期' },
                { dimension: 'p', dimensionName: '按省份' },
                { dimension: 'b', dimensionName: '按业务线' }
            ];
        }
        //根据统计维度code获取所对应的名字
        function getName(code) {
            var name = '';
            if (code === 't') {
                name = '';
            } else if (code === 'p') {
                name = '各省';
            } else if (code === 'b') {
                name = '各业务线';
            }
            return name;
        }
        //格式化需求发送给后端的字段
        function fmtSendData(sendData, title) {
            var send = {
                startTime: '',
                endTime: '',
                dimension: '',
                title: ''
            };
            send.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd');
            send.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd');
            send.dimension = sendData.dimension;
            send.title = title;
            return send;
        }

        function fmtDateTitle(send) {
            var timeTitle = '';
            var startYear = send.startTime.substring(0, 4) + '年';
            var endYear = send.endTime.substring(0, 4) + '年';
            var startMonth = send.startTime.substring(5, 7) + '月';
            var endMonth = send.endTime.substring(5, 7) + '月';
            var startDay = send.startTime.substring(8, 10) + '日';
            var endDay = send.endTime.substring(8, 10) + '日';
            var booleanYear = startYear === endYear;
            var booleanMonth = startMonth === endMonth;
            var booleanDay = startDay === endDay;
            timeTitle = booleanYear && booleanMonth && booleanDay ? startYear + startMonth + startDay : startYear + startMonth + startDay + ' 至 ' + endYear + endMonth + endDay;
            return timeTitle;
        }
        //柱状图
        function getEchartsData(xAxisData, seriesData, title, legendName) {
            return {
                title: {
                    text: title,
                    x: 'center'
                },
                animation: false,
                tooltip: {
                    trigger: 'item'
                },
                grid: {
                    x2: 100
                },
                legend: {
                    data: [legendName],
                    orient: 'vertical',
                    x: 'right',
                    y: 30
                },
                xAxis: [{
                    type: 'category',
                    data: xAxisData
                }],
                yAxis: [{
                    type: 'value'
                }],
                series: [{
                    name: legendName,
                    type: 'bar',
                    data: seriesData
                }]
            };
        }
        //饼图
        function getEchartsPieData(items, dimension, title) {
            var seriesData = [];
            if (dimension === 'p') {
                angular.forEach(items, function(item) {
                    seriesData.push({ value: item.churnRate.substring(0, item.churnRate.length - 1), name: item.provinceCodeName });
                });
            } else {
                angular.forEach(items, function(item) {
                    seriesData.push({ value: item.churnRate.substring(0, item.churnRate.length - 1), name: item.businessLineName });
                });
            }
            var option = {
                title: {
                    text: title,
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{b} <br/>用户流失占比:{c}%'
                },
                animation: false,
                series: [{
                    type: 'pie',
                    radius: '70%',
                    center: ['50%', '50%'],
                    /*itemStyle : {
                        normal : {
                          label : {
                            position : 'inner',
                            formatter: function(param){
                              return param.name+'\n'+param.percent+'%';
                            }
                          },
                          labelLine : {
                            show : false
                          }
                        }
                       },*/
                    data: seriesData
                }]
            };
            return option;
        }
        //导出
        function exportView(myEcharts1, myEcharts2, send) {
            send.base64s = [];
            var outData = '';
            var b = myEcharts1.getDataURL('png');
            var p = myEcharts2.getDataURL('png');
            send.base64s.push(b.split(',')[1]);
            send.base64s.push(p.split(',')[1]);
            outData = JSON.stringify(send);
            return outData;
        }

        function getFormatterFn(str) {
            return function(params) {
                var name = params.name;
                var seriesName = params.seriesName;
                var value = params.value;
                return seriesName + ' </br>' + name + ':' + value + str;
            };
        }

        function formatterYAxis(yName, yFormatter) {
            return {
                name: yName,
                axisLabel: {
                    formatter: yFormatter
                }
            };
        }
    }
})();
